package e2q.security.web.manage.message;

import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import e2q.di.web.util.MvcUtil;
import e2q.security.domain.E2qSmUser;
import e2q.security.domain.E2qSmsSendHistory;
import e2q.security.web.attributes.PageInfo;
import e2q.security.web.attributes.SmsSearchPageInfo;
import e2q.web.orm.ICommonDao;
import e2q.web.session.Pagination;

@Controller
public class MessageController {
	@Autowired
	@Qualifier("commonDAOProxy")
	private ICommonDao dao;
	


	@RequestMapping("/sms/userList.do")
	public ModelAndView smsUserList(@ModelAttribute("pageInfo")
			PageInfo pageInfo) {		
		Pagination pagination = new Pagination();
		pagination.set(pageInfo.getStart(), pageInfo.getLimit());

		String searchSql = "select role.e2qSmUser from E2qSmUserRole role where role.e2qSmRole.id=?";

		List<?> dataList = dao.find(pagination, searchSql,new Object[]{1276313092391l});

		JSONObject jsonObject = new JSONObject();

		jsonObject.element("totalCount", pagination.getTotalResults());

		if (dataList.size() > 0) {
			JSONArray data = new JSONArray();

			for (Object d : dataList) {
				JSONObject item = new JSONObject();
				E2qSmUser field = (E2qSmUser) d;
				item.element("id", field.getId());
				item.element("userRealName", MvcUtil.toJsonString(field
						.getUserRealName()));
				item.element("userDepartmentId", MvcUtil.toJsonString(field
						.getE2qSmDepartment().getId()));
				item.element("e2qSmDepartmentName", MvcUtil.toJsonString(field
						.getE2qSmDepartment().getDepaName()));
				data.add(item);
			}

			jsonObject.element("data", data);
		}

		return MvcUtil.jsonObjectModelAndView(jsonObject);
	}
	
	
	@RequestMapping("/sms/smsList.do")
	public ModelAndView smsList(@ModelAttribute("pageInfo")
			SmsSearchPageInfo pageInfo) {		
		Pagination pagination = new Pagination();
		pagination.set(pageInfo.getStart(), pageInfo.getLimit());

		String searchSql = getSql(pageInfo);
//		Object[] objects;
//		if(pageInfo.getBeginTime()==null&&pageInfo.getEndTime()==null){
//			objects=new Object[]{};
//		}else if (pageInfo.getBeginTime()!=null&&pageInfo.getEndTime()==null) {
//			objects=new Object[]{pageInfo.getBeginTime()};
//		}else if (pageInfo.getBeginTime()==null&&pageInfo.getEndTime()!=null) {
//			objects=new Object[]{pageInfo.getEndTime()};
//		}else {
//			objects=new Object[]{pageInfo.getBeginTime(),pageInfo.getEndTime()};
//		}

		List<?> dataList = dao.find(pagination, searchSql);

		JSONObject jsonObject = new JSONObject();

		jsonObject.element("totalCount", pagination.getTotalResults());

		if (dataList.size() > 0) {
			JSONArray data = new JSONArray();

			for (Object d : dataList) {
				JSONObject item = new JSONObject();
				E2qSmsSendHistory field = (E2qSmsSendHistory) d;
				item.element("id", field.getId());
				item.element("shcSendUserId", MvcUtil.toJsonString(field
						.getShcSendUserId()));
				item.element("shcSendUserName", MvcUtil.toJsonString(field
						.getShcSendUserName()));
				item.element("shcReceiverId", MvcUtil.toJsonString(field
						.getShcReceiverId()));
				item.element("shcReceiverMobiles", MvcUtil.toJsonString(field
						.getShcReceiverMobiles()));
				item.element("shcContent", MvcUtil.toJsonString(field
						.getShcContent()));
				item.element("shcNode", MvcUtil.toJsonString(field
						.getShcNode()));
				data.add(item);
			}

			jsonObject.element("data", data);
		}

		return MvcUtil.jsonObjectModelAndView(jsonObject);
	}
	
	private String getSql(SmsSearchPageInfo pageInfo) {
		String sqlcon = "from E2qSmsSendHistory o where 1=1 ";
		if (pageInfo.getId() != null) {
			sqlcon = sqlcon + " and o.shcSendUserId = "
					+ pageInfo.getId();
		}
		if (pageInfo.getBeginTime() != null && !pageInfo.getBeginTime().isEmpty()) {
			sqlcon = sqlcon + " and o.shcSendDate > to_date('"+pageInfo.getBeginTime()+"','yyyy-mm-dd')";
//					+ pageInfo.getBeginTime() ;
		}
		if (pageInfo.getEndTime() != null && !pageInfo.getEndTime().isEmpty()) {
			sqlcon = sqlcon + " and o.shcSendDate < to_date('"+pageInfo.getEndTime()+"','yyyy-mm-dd')";
//					+ pageInfo.getEndTime();
		}
		
		
			return sqlcon + " and o.shcState = "+E2qSmsSendHistory.SENDED;
		
	}
	
}
